package com.snopy.concurrency.interrupt;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author snopy
 * @version 1.0.0
 * @classname DemoInterrupt
 * @description
 * @email 77912204@qq.com
 * @date 2022/07/04 8:21
 */
@Slf4j
public class DemoInterrupt {
    static ExecutorService service1 = Executors.newFixedThreadPool(2);

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            log.debug("t1 begin...");
            try {
                synchronized (DemoInterrupt.class){
                    DemoInterrupt.class.wait();
                }

            } catch (Exception e) {
                log.debug(" thread's interrupt state:{}",
                        Thread.currentThread().isInterrupted());
                e.printStackTrace();
            }
        },"t1");
        Thread t2 = new Thread(()->{
            log.debug("t2 begin...");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t2");
        t1.start();
        log.debug("going to interrupt: thread's interrupt state:{}",t1.isInterrupted());
        t1.interrupt();
        log.debug("interrupted: thread's interrupt state:{}",t1.isInterrupted());

    }
}
